package com.satan.vote.interceptor;

import com.satan.vote.util.JwtUtils;
import com.satan.vote.util.StringUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @ClassName SysInterceptor
 * @Description TODO
 * @date 2023/8/12 12:26
 * @Version 1.0
 */
@Component
public class SysInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path=request.getRequestURI();
        System.out.println("path="+path);
        if(handler instanceof HandlerMethod){
            String token = request.getHeader("token");
            System.out.println("token="+token);
            if(StringUtil.isEmpty(token)){
                System.out.println("token为空！");
                throw new RuntimeException("签名验证不存在！");
            }else{
                Claims claims = JwtUtils.validateJWT(token).getClaims();
                if(claims!=null){
                    System.out.println("鉴权成功");
                    return true;
                }else{
                    System.out.println("鉴权失败");
                    throw new RuntimeException("鉴权失败！");
                }
            }
        }else{
            return true;
        }

    }
}
